var user = {};
var db = require('../../database');
var rs = require('../module/respond');

user.signin = function (req,res) {
    var tj = [], stj = '';
    var list = rs.copy(rs);
    if (req.body.username && req.body.pwd) {
        stj = 'where username=? and pwd=?'
        tj.push(req.body.username);
        tj.push(req.body.pwd);
        db.query('select * from user ' + stj, tj, function (err, rows) {
            // console.log(rows);
            if (err) {
                console.log(err)
            }
            else {

                if (rows.length == 1) {
                    list.respond.msg = '登录成功';
                    list.respond.returnCode = '0000';
                    req.session['uid'] = rows[0].id;
                    req.session['name'] = rows[0].name;
                    req.session['type'] = rows[0].type;
                    //console.log(rows,req.session)
                } else {
                    list.respond.msg = '您还未注册';
                    list.respond.returnCode = '0011';
                }


            }
            res.json(list.respond);
        })
    } else {
        // list.status = 0
        // list.success = 0
        // list.msg = '提交数据有误'
        res.send(rs.fail)
    }


}

user.signup = function (req,res) {
    var tj = [];
    var list = rs.copy(rs);
    if (req.body.name && req.body.password) {

        tj.push(req.body.name)
        tj.push(req.body.password)

        new Promise(function(resolve, reject) {

            db.query('INSERT INTO user (username, pwd) VALUES(?,?)', tj, function (err, rows) {
                if (err) {
                    if (err.sqlState = 23000) {
                        list.fail.msg = '您已经注册过了'
                    }
                    res.send(list.fail)
                }
                else {

                    if (rows.insertId) {
                        return resolve(rows.insertId)

                    } else {
                        list.fail.msg = '注册失败'
                        res.send(list.fail)
                    }
                }
            })
        }).then(function (id) {
            db.query('INSERT INTO userinfo (uid) VALUES(?)',[id],function (err,rows) {
                if (rows.insertId) {
                    list.success.msg = '注册成功';
                    res.send(list.success)
                } else {
                    list.fail.msg = '注册失败';
                    res.send(list.fail)
                }
            })
        })

    } else {
        list.fail.msg = '提交数据有误';
        res.send(list.fail)
    }
}

user.getInfo = function (req,res) {
    var id = req.session['uid'];
    var list = rs.copy(rs);
    db.query('select nickname , avatar , sex , birthday , phone from userinfo where uid = '+id,function (err,row) {
        if(err){
            console.log(err)
        }else{
            list.success.content = row[0];
            res.send(list.success)
        }
    })
}

user.setInfo = function (req,res) {
    var id = req.session['uid'];
    var tj = [];
    var list = rs.copy(rs);
    console.log(list)
    if(req.body.nickname && req.body.sex && req.body.birthday && req.body.phone){
        tj.push(req.body.nickname);
        tj.push(req.body.sex);
        tj.push(req.body.birthday);
        tj.push(req.body.phone);
        db.query('update userinfo set nickname=? , sex=? , birthday=? , phone=? where uid = '+id,tj,function (err,row) {
            if(err){
                console.log(err)
            }else{
                list.success.msg = '修改成功';
                res.send(list.success)
            }
        })
    }else{
        res.send(list.fail)
    }

}

user.avatar = function (req,res) {

}

module.exports = user;